package com.nd.android.u.ui.chatInterfaceImpl;


import ims.IMSdk;
import ims.cmd.PersonNotifyCmd;
import ims.manager.IMSStateManager;
import ims.utils.IMLogUtils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;

import com.common.StarAppCIConst;
import com.common.android.utils.task.genericTask.TaskParams;
import com.common.widget.CommonTipDlg;
import com.common.widget.CommonTipDlg.OnCommonDlgClickListener;
import com.nd.android.u.business.db.dao.RecentContactRecords;
import com.nd.android.u.commonWidget.NotificationMsg;
import com.nd.android.u.controller.ChatConst.LocalMessageType;
import com.nd.android.u.controller.ChatGlobalVariable;
import com.nd.android.u.controller.bean.DefaultGroup;
import com.nd.android.u.controller.bean.contact.RecentContactItem;
import com.nd.android.u.controller.bean.event.UnreadCountChangedEvent;
import com.nd.android.u.controller.factory.AppMessageFactory;
import com.nd.android.u.controller.factory.ContactFactory;
import com.nd.android.u.controller.innerInterface.IMessageDisplay;
import com.nd.android.u.controller.observer.MessageDispatcher;
import com.nd.android.u.controller.utils.CommonUtils;
import com.nd.android.u.imSdk.GroupLoginManager;
import com.nd.android.u.imSdk.GroupSystemMsgReceiver;
import com.nd.android.u.imSdk.IMSdkCall;
import com.nd.android.u.ui.R;
import com.nd.android.u.ui.activity.message_chat.ChatActivity_Group;
import com.nd.android.u.ui.activity.message_chat.ChatActivity_Person;
import com.nd.android.u.ui.activity.message_chat.ClearMessageHistroyCallBack;
import com.nd.android.u.ui.activity.message_chat.DelChatRecordTaskListener;
import com.nd.android.u.ui.beanStarApp.AppContactItem;
import com.nd.android.u.ui.beanStarApp.DisplayMessage_App;
import com.nd.android.u.ui.beanStarApp.DisplayMessage_Group;
import com.nd.android.u.ui.beanStarApp.DisplayMessage_Person;
import com.nd.android.u.ui.beanStarApp.GroupContactItem;
import com.nd.android.u.ui.chatUiUtils.AudioQuenePlayManager;
import com.nd.android.u.ui.widge.PopQuickReply;
import com.product.android.business.Const;
import com.product.android.commonInterface.BaseCommonStruct;
import com.product.android.commonInterface.CIConst;
import com.product.android.commonInterface.ICommonInterObserver;
import com.product.android.commonInterface.chat.ChatConst;
import com.product.android.commonInterface.chat.ChatGroup;
import com.product.android.commonInterface.contact.OapGroup;

import de.greenrobot.event.EventBus;

public class ChatCommonInterImpl implements ICommonInterObserver{
	
	@Override
	public int onCommonInterModelProc(int id, BaseCommonStruct msg) {
		int ret = CIConst.COM_RET_ERROR;
		switch(id){
//			case CIConst.CHAT_ACCEPT_TASK_20001:
//			case CIConst.CHAT_FINISH_TASK_20002:
//				//onAcceptTaskResult
//				if (msg==null || msg.obj1==null || !(msg.obj1 instanceof ErpInfo)){
//					return CIConst.COM_RET_PARA_ERROR;
//				}
//				ErpInfo info = (ErpInfo)msg.obj1;
//				IMessageDisplay message=MessageFactory.INSTANCE.getPersonMessage();
//				message.setGenerateId(info.generateId);
//				MessageDispatcher.getInstance().notifyMessageStateChanged(message, msg.iPara);
//				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_TRANSFER_20003:
//转让群
//20003
				if (msg==null || msg.lPara <= 0 || TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				String mgenkey=CommonUtils.getGroupOperationKey(msg.sPara,Const.GROUP_OP.TRANSFER);
				ChatGlobalVariable.getInstance().getFackBackList().add(mgenkey);
				
				//groupKey=msg.sPara;groupType=msg.iPara;gid=msg.lPara;
				IMSdkCall.GroupMessage.transferGroup(msg.sPara, msg.iPara, msg.lPara);
				
				msg.obj2 = mgenkey;
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_REQUEST_JOIN_20004:
//申请加入群（网龙版无用）
//20004
				if (msg==null || msg.lPara==0){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				//gid=msg.lPara,comment=msg.sPara
				IMSdkCall.GroupMessage.addGroupRequest(msg.lPara,msg.sPara);
				return CIConst.COM_RET_OK;
//			case CIConst.CHAT_GROUP_DELETE_HISTORY_20005:
////删除聊天记录
////20005
//				if (msg==null || msg.obj1 == null || msg.lPara==0 || !(msg.obj1 instanceof Context)){
//					return CIConst.COM_RET_PARA_ERROR;
//				}
//				int groupType=msg.iPara;
//				DisplayMessage_Group groupMessage=new DisplayMessage_Group(msg.lPara+"",groupType);
//				DeleteHistoryDialog dialog=new DeleteHistoryDialog((Context) msg.obj1, groupMessage);
//				dialog.show();
//				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_ADD_MEMBER_20006:
//添加群成员				
//20006						
				if (msg==null || msg.obj1==null || !(msg.obj1 instanceof long[])){
					return CIConst.COM_RET_PARA_ERROR;
				}
				int wseqadd=IMSdk.getWseq();
				String specialaddgenkey=CommonUtils.getGroupOperationKey(Const.GROUP_OP.ADD_MEMBER,","+wseqadd);
				ChatGlobalVariable.getInstance().getFackBackList().add(specialaddgenkey);
				
				//groupType=msg.iPara, uids=msg.obj1
				IMSdkCall.GroupMessage.group_cmd_c2n_add_member(msg.sPara,msg.iPara, (long[]) msg.obj1,wseqadd);

				msg.obj2 = specialaddgenkey;
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_DELETE_MEMBER_20007:
//删除群成员（需要开线程15秒后通知界面结果，类似群转让）
//20007
				if (msg==null || msg.lPara==0 || TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR;
				}
				long fid=0;
				try {
					fid= Long.valueOf(msg.sPara);
				} catch (Exception e) {
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (fid==0){
					return CIConst.COM_RET_PARA_ERROR;
				}
				int wseq=IMSdk.getWseq();
                //根据wseq反馈
				String specialgenkey=CommonUtils.getGroupOperationKey(Const.GROUP_OP.DEL_MEMBER,","+wseq);
				ChatGlobalVariable.getInstance().getFackBackList().add(specialgenkey);				
				//gid=msg.lPara
				IMSdkCall.GroupMessage.group_cmd_c2n_remove_member(msg.lPara+"",OapGroup.NORMALGROUP,fid,wseq);
				msg.obj2 = specialgenkey;
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_QUIT_20008:
//退出群+反馈
//20008
				if (msg==null ||  msg.lPara==0){
					return CIConst.COM_RET_PARA_ERROR;
				}
				int quit_type=msg.iPara;
				long quit_gid=msg.lPara;
				if(quit_type==ChatConst.CHAT_TYPE_DISCUSSION){
					GroupContactItem item = new GroupContactItem();           //删除群记录
					item.setId(String.valueOf(quit_gid));
					item.setSubtype(ChatGroup.getDiscussionGroupType());
					RecentContactRecords.INSTANCE.deleteItem(item);
					IMSdkCall.GroupMessage.quitGroup(quit_gid+"", ChatGroup.getDiscussionGroupType());
				}else{
					String mGenQuitGroupkey=CommonUtils.getGroupOperationKey(quit_gid+"",Const.GROUP_OP.QUIT_GROUP);
					ChatGlobalVariable.getInstance().getFackBackList().add(mGenQuitGroupkey);
					IMSdkCall.GroupMessage.quitGroup(quit_gid+"", ChatGroup.getNormalGroupType());
					msg.obj2 = mGenQuitGroupkey;
				}
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_DISMISS_20009:
// 解散群 +反馈
//20009
				if (msg==null || msg.lPara==0){
					return CIConst.COM_RET_PARA_ERROR;
				}
				String mGenDimissGroupkey=CommonUtils.getGroupOperationKey(msg.lPara+"",Const.GROUP_OP.DISMISS_GROUP);
				ChatGlobalVariable.getInstance().getFackBackList().add(mGenDimissGroupkey);
				//gid=msg.lPara,目前仅有一种普通群可解散,发IMS命令
				IMSdkCall.GroupMessage.dimssGroup(msg.lPara+"", ChatGroup.getNormalGroupType());
				msg.obj2 = mGenDimissGroupkey;
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_LOGIN_20010:
//登录指定群
//200010
				if (msg==null){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				if (TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR;
				}
				//groupKey=msg.sPara;grouptype=msg.iPara
				new DefaultGroup(msg.sPara, msg.iPara).loginInit();
//				if (!msg.bPara){
//					//不清旧数据
//					GroupLoginManager.getInstance().loginFixGroup(msg.sPara, msg.iPara);
//				}else{
//					GroupLoginManager.getInstance().loginInitFixGroup(msg.sPara, msg.iPara);
//				}
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_LOGIN_ALL_20011:
//登录所有群
//20011
				if (msg==null){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				GroupLoginManager.INSTANCE.loginAllGroup(false);
//				if (!msg.bPara){
//					//不清旧数据
//					GroupLoginManager.getInstance().loginAllGroups();
//				}else{
//					GroupLoginManager.getInstance().loginInitAllGroups();
//				}
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_LOGOUT_FIX_20012:
// 登出指定群
//20012
				if (msg==null || TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				//groupKey=msg.sPara;grouptype=msg.iPara
				DefaultGroup group1=new DefaultGroup(msg.sPara, msg.iPara);
				group1.logout();
//				GroupLoginManager.getInstance().logoutFixGroup(msg.sPara, msg.iPara);
				if (msg.bPara){
					//需要清掉聊天记录、删除最近联系人
					GroupContactItem logoutItem=(GroupContactItem) ContactFactory.INSTANCE.getRecentContactItem(LocalMessageType.GROUP);
					logoutItem.setId(msg.sPara);
					logoutItem.setSubtype(msg.iPara);
					RecentContactRecords.INSTANCE.deleteItem(logoutItem);
				}
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_MODIFY_PERMISSION_20013:
// 更新加群的限制（允许任何人/需要验证/不允许）
//20013		
				if (msg==null || msg.lPara<=0){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				IMSdkCall.GroupMessage.sendModifyJoinperm(msg.lPara, msg.iPara);
				break;
			case CIConst.CHAT_GROUP_MODIFY_INTRODUCE_20014:
// 修改群简介
//20014
				if (msg==null){
					return CIConst.COM_RET_PARA_ERROR;
				}
				int wintroductseq=IMSdk.getWseq();
				String groupintroductkey=CommonUtils.getGroupOperationKey(Const.GROUP_OP.MODIFY_NOTICE,","+wintroductseq);	
				ChatGlobalVariable.getInstance().getFackBackList().add(groupintroductkey);
				//introduce=msg.sPara;gid=msg.lPara,仅普通群有此操作
				IMSdkCall.GroupMessage.sendModifyIntroduction(msg.lPara,ChatGroup.getNormalGroupType(),msg.sPara,wintroductseq);
				msg.obj2=groupintroductkey;
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_MODIFY_NOTICE_20015:
// 修改群公告 讨论组主题
//20015
				if (msg==null){
					return CIConst.COM_RET_PARA_ERROR;
				}
				//notice=msg.sPara;gid=msg.lPara,仅普通群有此操作
				int wnoticeseq=IMSdk.getWseq();
                //根据wseq反馈
				String groupNoticekey=CommonUtils.getGroupOperationKey(Const.GROUP_OP.MODIFY_NOTICE,","+wnoticeseq);	
				ChatGlobalVariable.getInstance().getFackBackList().add(groupNoticekey);
				IMSdkCall.GroupMessage.sendModifyGroupNotice(msg.lPara,msg.iPara,msg.sPara,wnoticeseq);

				msg.obj2=groupNoticekey;
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_SHARE_22016:
// 打开群共享界面
//22016
//				if (msg==null || TextUtils.isEmpty(msg.sPara) || msg.obj1==null || !(msg.obj1 instanceof Context) ){
//					return CIConst.COM_RET_PARA_ERROR;
//				}
//				long share_gid=0;
//				try {
//					share_gid=Long.valueOf(msg.sPara);
//				} catch (Exception e) {
//					return CIConst.COM_RET_PARA_ERROR;
//				}
//				int catagory=msg.iPara;
//				long ownerid=msg.lPara;
//				Bundle shareBundle=new Bundle();
//				shareBundle.putLong("ownerid", ownerid);
//				shareBundle.putLong(com.nd.android.u.controller.ChatConst.KEY.GID, share_gid);
//				shareBundle.putInt("category", catagory);
//				Context context=(Context) msg.obj1;
//				Intent intentFileList=new Intent(context, FileListActivity.class);
//				intentFileList.putExtras(shareBundle);
//				context.startActivity(intentFileList);
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_MASK_TO_RECEIVE_20017:
// 屏蔽转接收群消息
//20017
				if (msg==null || TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				//groupKey=msg.sPara,groupType=msg.iPara
				new DefaultGroup(msg.sPara, msg.iPara).loginInit();
//				GroupLoginManager.getInstance().MASK2ReceiveLogRecGroup(msg.sPara, msg.iPara);
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GROUP_REMOVE_FEEDBACK_KEY_20018:
// 删除反馈列表的key
//20018
				if (msg==null || TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR;
				}
				//key=msg.iPara
				ChatGlobalVariable.getInstance().getFackBackList().remove(msg.sPara);
				return CIConst.COM_RET_OK;
				
			case CIConst.CHAT_CHANGE_IMAGE_20059:
// 头像修改或者形象照修改
//20059
				if (msg==null ){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				int is_avata=msg.iPara;
				int sys_avata=(int) msg.lPara;
				IMSdkCall.PersonMessage.changeUserAvatar(is_avata, sys_avata);
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_TO_CHAT_ACTIVITY_22060:
// 打开聊天界面
//20260
				if (msg==null || msg.obj1 == null || msg.lPara==0 || !(msg.obj1 instanceof Context)){
					return CIConst.COM_RET_PARA_ERROR;
				}
		        Intent intent = new Intent();
		        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
				Context ctx=(Context) msg.obj1;
				int type=msg.iPara;
				long chat_id=msg.lPara;
				int gType=0;
				Bundle bundle=new Bundle();
				switch (type) {
				case ChatConst.CHAT_TYPE_PERSON:
					if (msg.obj2!=null && msg.obj2 instanceof String[]){
						String[] quickReplays=(String[]) msg.obj2;
						if (quickReplays!=null){
							bundle.putInt("quick_reply_type", PopQuickReply.REPLY_TYPE_PRESENT);
							bundle.putStringArray(com.nd.android.u.controller.ChatConst.KEY.QUICKREPLY, quickReplays);
						}
					}
				    intent.setClass(ctx,ChatActivity_Person.class);
					bundle.putLong(com.nd.android.u.controller.ChatConst.KEY.FID, chat_id);
					intent.putExtras(bundle);
					ctx.startActivity(intent);
					return CIConst.COM_RET_OK;
				case ChatConst.CHAT_TYPE_NORMAL_GROUP:
					gType=ChatGroup.getNormalGroupType();
					break;
				case ChatConst.CHAT_TYPE_DISCUSSION:
					gType=ChatGroup.getDiscussionGroupType();
					break;
				case ChatConst.CHAT_TYPE_DEPARTMENT:
				case ChatConst.CHAT_TYPE_CLASS:
					gType=ChatGroup.getDepartGroupType();
					break;
				default:
					return CIConst.COM_RET_NO_ID_DIFINE;
				}
				bundle.putLong(com.nd.android.u.controller.ChatConst.KEY.GID, chat_id);
	            bundle.putInt(com.nd.android.u.controller.ChatConst.KEY.GROUPTYPE, gType);
				intent.setClass(ctx,ChatActivity_Group.class);
				intent.putExtras(bundle);
				ctx.startActivity(intent);
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_ADD_FRIEND_20062:
//请求添加好友（网龙版无用）
//20062
				if (msg==null || msg.lPara==0){
					return CIConst.COM_RET_PARA_ERROR;
				}
				if (!IMSStateManager.getInstance().isNetworkAvailable()){
					return CIConst.COM_RET_NO_NETWORK;
				}
				if (!IMSStateManager.getInstance().isOnline()){
					return CIConst.COM_RET_IMS_OFFLINE;
				}
				//gid=msg.lPara,comment=msg.sPara
				IMSdkCall.PersonMessage.sendNotify(PersonNotifyCmd.MSG_AUTH_REQUEST, msg.lPara,msg.sPara); 
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_ACK_MESSAGE_20066:
//清空未读消息数
//20066
				if (msg == null || msg.obj1 == null
						|| !(msg.obj1 instanceof String)) {
					return CIConst.COM_RET_PARA_ERROR;
				}
				IMessageDisplay display=AppMessageFactory.INSTANCE.getAppMessage(msg.iPara, (String) msg.obj1);
				if (display==null){
					IMLogUtils.e(IMLogUtils.CHAT,"unknow app:"+msg.iPara+",code="+msg.obj1);
					return CIConst.COM_RET_ERROR;
				}
				AppContactItem contactItem = (AppContactItem) ContactFactory.INSTANCE.getRecentContactItem(LocalMessageType.APP);
				if (contactItem==null){
					IMLogUtils.e(IMLogUtils.CHAT,"unknow app:"+msg.iPara+",code="+msg.obj1);
					return CIConst.COM_RET_ERROR;
				}
				display.getProccessInterface().ackAllMessage();	
                contactItem.setId(msg.iPara + "");
                contactItem.setCode((String) msg.obj1);
				RecentContactRecords.INSTANCE.clearUnreadCount(contactItem);
				EventBus.getDefault().post(new UnreadCountChangedEvent());
				NotificationMsg.getInstance().callbackSetNotiType();
				return CIConst.COM_RET_OK;			
			case CIConst.CHAT_SEND_BIRTHDAY_REMAIN_MESSAGE_20067:
//发送生日提醒消息
//20067
//				if (msg == null || msg.sPara == null) {
//					return CIConst.COM_RET_PARA_ERROR;
//				}

//FIXME 无生日提醒
//	            CommonInterfaceManager.INSTANCE.CommonInterfaceID(
//	                    CIConst.MAIN_MODELID,
//	                    CIConst.MAIN_SEND_BIRTHDAY_REMAIN_MESSAGE_90008, msg);
				
//				AppMessage message = new AppMessage();
//				message.setAppid(Configuration.MYAPPID);
//				message.setCode(Configuration.BIRTHDAYREMINDCODE);
//				message.setTime(IMSdk.computeServertime());
//				message.setContent(msg.sPara);
//				message.setData(msg.sPara);
//				MessageDispatcher.getInstance().dipatchMessage(message, ImsMessage.APP_MSG_TYPE, false);
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_RECENTLY_LIST_20068:
//获取最近联系人列表
//20068
				List<Long> ids = new ArrayList<Long>();
				List<RecentContactItem> list = RecentContactRecords.INSTANCE.getList();
				for (RecentContactItem item: list) {
					if (item.getMessageType() == LocalMessageType.PERSON) {
						ids.add(Long.valueOf(item.getId()));
					}
				}
				msg.obj2 = ids;
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GIF_DECODE_FINISH_20069:
				if (msg == null || msg.sPara == null) {
					return CIConst.COM_RET_PARA_ERROR;
				}
//				MessageDispatcher.getInstance().notifyMessageStateChanged(msg.sPara,ImsMessage.GIF_DECODE_SUCCESS);
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_CLEAR_UNREAD_APP_MESSAGE_20070:
				if (msg==null || msg.iPara<=0 || TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR; 
				}
				IMessageDisplay display1=AppMessageFactory.INSTANCE.getAppMessage(msg.iPara, msg.sPara);
				if (display1==null){
					IMLogUtils.e(IMLogUtils.CHAT,"unknow app:"+msg.iPara+",code="+msg.sPara);
					return CIConst.COM_RET_ERROR;
				}
				AppContactItem item = (AppContactItem) ContactFactory.INSTANCE.getRecentContactItem(LocalMessageType.APP);
				if (item==null){
					IMLogUtils.e(IMLogUtils.CHAT,"unknow app:"+msg.iPara+",code="+msg.sPara);
					return CIConst.COM_RET_ERROR;
				}
				item.setId(String.valueOf(msg.iPara));
				item.setCode(msg.sPara);
				RecentContactRecords.INSTANCE.clearUnreadCount(item);
				display1.getProccessInterface().ackAllMessage();
				return CIConst.COM_RET_OK;
			case CIConst.CHAT_GET_UNREAD_APP_MESSAGE_20071:
				if (msg==null || msg.iPara<=0 || TextUtils.isEmpty(msg.sPara)){
					return CIConst.COM_RET_PARA_ERROR; 
				}
				AppContactItem unreadItem=new AppContactItem();
				unreadItem.setId(String.valueOf(msg.iPara));
				unreadItem.setCode(msg.sPara);
				unreadItem = (AppContactItem) RecentContactRecords.INSTANCE.getItem(unreadItem);
				if (unreadItem == null) {
					msg.iPara = -1;
				} else {
					msg.iPara = unreadItem.getUnreadCount();
				}
				return CIConst.COM_RET_OK;
			case StarAppCIConst.CHAT_NOTIFY_USER_NAME:
				if (msg==null){
					return CIConst.COM_RET_PARA_ERROR; 
				}
				if (msg.bPara){
					Message message=new Message();
					message.what=StarAppCIConst.CHAT_NOTIFY_USER_NAME;
					message.obj=msg.sPara;
					bundle = new Bundle();
					bundle.putLong(com.nd.android.u.controller.ChatConst.KEY.FID, msg.lPara);
					message.setData(bundle);
					MessageDispatcher.getInstance().notifyOtherMessage(LocalMessageType.PERSON, message);
				}
				return CIConst.COM_RET_OK;
			case StarAppCIConst.CHAT_NOTIFY_GROUP_NAME_210003:
				if (msg==null || msg.lPara<=0){
					return CIConst.COM_RET_PARA_ERROR; 
				}
				Message message=new Message();
				message.what=StarAppCIConst.CHAT_NOTIFY_GROUP_NAME_210003;
				message.obj=msg.sPara;
				bundle = new Bundle();
				bundle.putLong(com.nd.android.u.controller.ChatConst.KEY.GID, msg.lPara);
				message.setData(bundle);
				MessageDispatcher.getInstance().notifyOtherMessage(LocalMessageType.GROUP, message);
				return CIConst.COM_RET_OK;
			case StarAppCIConst.CHAT_NOTIFY_GROUP_REMOVE_210004:
				if (msg==null || msg.lPara<=0){
					return CIConst.COM_RET_PARA_ERROR; 
				}
				//lpara放着gid，ipara放着grouptype
				GroupSystemMsgReceiver.notifyGroupDel(msg.lPara+"", msg.iPara);
				
				break;
		case CIConst.CHAT_GROUP_DELETE_HISTORY_20005:
		case StarAppCIConst.CHAT_NOTIFY_DELETE_PERSON_HISTORY_210005:
		case StarAppCIConst.CHAT_NOTIFY_DELETE_SYSTEM_HISTORY_210007:
			if (msg == null || msg.lPara <= 0 || msg.obj1 == null
					|| !(msg.obj1 instanceof Context)) {
				return CIConst.COM_RET_PARA_ERROR;
			}
			// DisplayMessage_Person personMessage=new DisplayMessage_Person();
			// personMessage.setFromUid(msg.lPara);
			// DeleteHistoryDialog delDialog=new DeleteHistoryDialog((Context)
			// msg.obj1, personMessage);
			// delDialog.show();
			final Context context = (Context) msg.obj1;
			final long delItemId = msg.lPara;
			CommonTipDlg commonTipDlg = new CommonTipDlg(context,
					context.getString(R.string.chat_confirm_clear),
					context.getString(R.string.cancel),
					context.getString(R.string.confirm),
					R.style.common_dialog_style);
			commonTipDlg.setLayoutResID(R.layout.common_tip_dlg_pink);
			DeleteChatRecordTask deleteTask = null;
			IMessageDisplay messages = null;
			if (msg.obj2 != null && msg.obj2 instanceof DisplayMessage_App) {// 应用消息清除
				messages = (DisplayMessage_App) msg.obj2;
			} else {
				if (msg.iPara == ChatGroup.getNormalGroupType()) {// 群聊清除
					int groupType = msg.iPara;
					messages = new DisplayMessage_Group(msg.lPara + "",
							groupType);
				} else {// 私聊清除
					DisplayMessage_Person temp = new DisplayMessage_Person();
					temp.setFromUid(msg.lPara);
					messages = temp;
				}
			}
			deleteTask = new DeleteChatRecordTask(messages);
			DelChatRecordTaskListener listener = new DelChatRecordTaskListener(
					deleteTask);
			ClearMessageHistroyCallBack mClearHistroyDlgCallBack = new ClearMessageHistroyCallBack(
					listener, messages);
			commonTipDlg.setOnListener(mClearHistroyDlgCallBack);
			commonTipDlg.setCanceledOnTouchOutside(true);
			commonTipDlg.show();
			return CIConst.COM_RET_OK;
			default:
				ret = CIConst.COM_RET_NO_ID_DIFINE;
				break;
		}
		return ret; 
		
	}

}
